var layer = layui.layer,
	laytpl = layui.laytpl,
	laydate = layui.laydate,
	$ = layui.jquery;


var id = 1;

$("input[value='添加']").click(add);

function add() {
	layer.open({
		type: 1,
		title: '信息添加',
		area: ['450px', '420px'],
		content: $("#testa").html(),
		success: function() {
			$("input[value='确定']").click(addtr);
			$("input[value='关闭']").click(closePage);
			renderDate("birth");
		}
	});
}


function addtr() {
	var name = $("input[name='name2']").val();
	var age = $("input[name='age']").val();
	var birth = $("input[name='birth']").val();
	var data = {
		id: id,
		name: name,
		age: age,
		birth: birth
	};
	var html = $("#tradd").html();
	var temp = laytpl(html).render(data);
	$("#tbody").append(temp);
	// console.log(name+"  "+age);
	id++;
	closePage();
}


function closePage() {
	layer.close(layer.index);
}

function renderDate(id) {
	laydate.render({
		elem: "#" + id,
		type: "data"
	});
}


function del(vid) {
	layer.confirm("确定删除？", function(index) {
		$("#tr" + vid).remove();
		layer.close(index);
	})
}

function upd(id) {
	layer.open({
		type: 1,
		content: $("#testa").html(),
		title: "信息修改",
		area: ['450px', '420px'],
		success: function() {
			var name = $("#tdb" + id).text();
			var age = $("#tdc" + id).text();
			var birth = $("#tdd" + id).text();
			$("input[name='name2']").val(name);
			$("input[name='age']").val(age);
			$("input[name='birth']").val(birth);
			$("input[value='关闭']").click(closePage);
			$("input[value='确定']").click({
				id: id
			}, testSave);
			renderDate("birth");
		}
	})

}

function testSave(event) {
	var id = event.data.id;
	$("#tdb" + id).html($("input[name='name2']").val());
	$("#tdc" + id).html($("input[name='age']").val());
	$("#tdd" + id).html($("input[name='birth']").val());
	closePage();
}

$("input[value='查询']").click(sel);

function sel() {
	$("tbody tr").css("background", "");
	var val = $("input[name='name']").val();
	if (val == '') return;
	$("tbody tr").each(function() {
		if ($(this).find("td[id^='tdb']").text().indexOf(val) > -1) {
			$(this).css("background", "red");
		}
	});
}
